#!/bin/bash

# Set the path of openssl executable and pyclaes installation path
OPENSSL=/usr/bin/openssl
GNUPLOT=/usr/bin/gnuplot


PYCLAES_PATH=../pyclaes
PYCLAES=./pyclaes.py
PYCLAES_PARAMS="--profiling --overlap --use-cpu"
#PYCLAES_PARAMS="--profiling --overlap"


DATA_TEMPLATE=data_template
DATE=$(date "+%d-%m-%y_%H:%M:%S")
#DATA=data_$DATE
#PLOT=plot_$DATE
DATA=data.dat
LATENCY=latency.dat
EXTIME=extime.dat
PLOT=plot
PLOT_PNG=./plot.png

KEY=foo__bar__foobar
HEX_KEY=666f6f5f5f6261725f5f666f6f626172

# Generate files containing random bytes with different size
for len in 16 64 256 1024 2048 4096 8192 16384 32768 65536 131072 262144 \
           524288 1048576 2097152 4194304 8388608 16777216 33554432 \
           67108864 134217728; do
  echo "Generating file with random bytes of size $len"
  $OPENSSL rand -out /tmp/test$len $len
done


# Prepare plotting files
cp $DATA_TEMPLATE $DATA

# Launch the encryption for each previously generated file
for len in 16 64 256 1024 2048 4096 8192 16384 32768 65536 131072 262144 \
           524288 1048576 2097152 4194304 8388608 16777216 33554432 \
           67108864 134217728; do

  pushd $PYCLAES_PATH
  row=$($PYCLAES $PYCLAES_PARAMS $KEY /tmp/test$len /tmp/enc_test$len)
  throughput=$(echo $row|awk 'BEGIN{FS="\n"}\
                              {if (match($0, "size: [0-9]+"))\
                                 { size=substr($0, RSTART+6, RLENGTH-6) }\
                              if (match($0, "throughput: ~[0-9]+.[0-9]+"))\
                                 { througput=substr($0, RSTART+13, RLENGTH-13)}\
                              }\
                              END{ print size, througput }')
  latency=$(echo $row|awk 'BEGIN{FS="\n"}\
                              {if (match($0, "size: [0-9]+"))\
                                 { size=substr($0, RSTART+6, RLENGTH-6) }\
                              if (match($0, "Latency: [0-9]+.[0-9]+(e-[0-9]+)*"))\
                                 { latency=substr($0, RSTART+9, RLENGTH-9)}\
                              }\
                              END{ print size, latency }')
  extime=$(echo $row|awk 'BEGIN{FS="\n"}\
                              {if (match($0, "size: [0-9]+"))\
                                 { size=substr($0, RSTART+6, RLENGTH-6) }\
                              if (match($0, "Execution time: [0-9]+.[0-9]+"))\
                                 { extime=substr($0, RSTART+16, RLENGTH-16)}\
                              }\
                              END{ print size, extime }')
  popd
  echo $row
  echo $throughput>>$DATA
  echo $latency>>$LATENCY
  echo $extime>>$EXTIME
done

# Check correctness of encrypted data
echo "Test correctness of encryption"
for len in 16 64 256 1024 2048 4096 8192 16384 32768 65536 131072 262144 \
           524288 1048576 2097152 4194304 8388608 16777216 33554432 \
           67108864 134217728; do

$OPENSSL aes-128-ecb -in /tmp/test$len -out /tmp/enc_openssl -nosalt -nopad  -iv 0 -K $HEX_KEY
cmp /tmp/enc_test$len /tmp/enc_openssl

if [ $? -ne 0 ]; then
   echo "ERROR: encryption of file of size $len different from openssl result!!!"
   # TODO: cleanup here
   exit 1
fi

done


# Plot data
echo "Plot results on plot_$DATE.png"
$GNUPLOT $PLOT

# Rename files
mv $PLOT_PNG plot_$DATE.png
mv $DATA data_$DATE.dat
mv $LATENCY latency_$DATE.dat
mv $EXTIME extime_$DATE.dat

# Cleanup
for len in 16 64 256 1024 2048 4096 8192 16384 32768 65536 131072 262144 \
           524288 1048576 2097152 4194304 8388608 16777216 33554432 \
           67108864 134217728; do
rm /tmp/enc_test$len
done

# Done
exit 0
